package com.maven.rabbitmq.direct;

import com.maven.rabbitmq.constant.Constant;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class DirectComsumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1. 建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("8.135.54.49");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        connectionFactory.setVirtualHost("rabbitmq");

        Connection connection = connectionFactory.newConnection();
        //2. 创建信道
        Channel channel = connection.createChannel();

        //3. 声明队列
        channel.queueDeclare(Constant.DIRECT_QUEUE_2, true, false, true, null);

        // 4. 消费消息
        DefaultConsumer defaultConsumer=new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println("DIRECT_QUEUE_1 接收到消息： " + msg);
            }
        };

        channel.basicConsume(Constant.DIRECT_QUEUE_2,true,defaultConsumer);

//        channel.close();
//        connection.close();

    }
}
